package com.example.demo.Impl;

import com.example.demo.entity.MbtiQuestion;
import com.example.demo.repository.MbtiQuestionRepository;
import com.example.demo.service.MbtiQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class MbtiQuestionServiceImpl implements MbtiQuestionService {

    @Autowired
    private MbtiQuestionRepository questionRepository;

    @Override
    public List<MbtiQuestion> getAllQuestions() {
        return questionRepository.findAll();
    }

    @Override
    public Optional<MbtiQuestion> getQuestionById(Long id) {
        return questionRepository.findById(id);
    }

    @Override
    public MbtiQuestion addQuestion(MbtiQuestion question) {
        return questionRepository.save(question);
    }

    @Override
    public MbtiQuestion updateQuestion(Long id, MbtiQuestion updated) {
        return questionRepository.findById(id).map(q -> {
            q.setText(updated.getText());
            q.setOptionA(updated.getOptionA());
            q.setOptionB(updated.getOptionB());
            q.setDimension(updated.getDimension());
            q.setDirection(updated.getDirection());
            return questionRepository.save(q);
        }).orElse(null);
    }

    @Override
    public boolean deleteQuestion(Long id) {
        if (questionRepository.existsById(id)) {
            questionRepository.deleteById(id);
            return true;
        }
        return false;
    }
}
